#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: ningbo_process.py
@time: 2018/1/4 14:54
"""

import sys

sys.path.append('..')
sys.path.append('../..')
from common.mongo import MongDb
from logger import Logger

MONGO_CONF = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('ningbo_process.log').get_logger()

source_db = MongDb(MONGO_CONF['host'], MONGO_CONF['port'], MONGO_CONF['db'],
                   MONGO_CONF['username'],
                   MONGO_CONF['password'], log=log)


# def get_change_date(origin_date):
#     time_obj = datetime.strptime(origin_date, '%Y年%m月%d日')
#
#     return time_obj.strftime('%Y-%m-%d')


def main():
    target_table_name = 'enterprise_data_gov'
    source_table_name = 'zhejiang_ningbo_2017'

    result_list = []
    count = 0
    for item in source_db.traverse_batch_field(source_table_name, {}, ['_id']):

        count += 1
        company = item.get("_id")
        data = {'_id': company}

        # try:
        #     register_date = item.get('register_date')
        #     if register_date is not None:
        #         change_date = get_change_date(register_date)
        #         data['register_date'] = change_date
        # except Exception as e:
        #     log.info("日期格式转换失败: company = {}".format(company))
        #     log.exception(e)

        result = source_db.find_one(target_table_name, {'company': company},
                                    ['address', 'industry', 'registered_capital', 'registered_capital_unit'])
        if result is not None:
            log.info("找到数据: company = {}".format(company))
            address = result.get("address")
            industry = result.get('industry')
            registered_capital = result.get('registered_capital')
            registered_capital_unit = result.get('registered_capital_unit')
            if address is not None:
                data['address'] = address
            if industry is not None:
                data['industry'] = industry
            if registered_capital is not None:
                data['registered_capital'] = registered_capital
            if registered_capital_unit is not None:
                data['registered_capital_unit'] = registered_capital_unit
        result_list.append(data)
        if len(result_list) >= 1000:
            source_db.insert_batch_data(source_table_name, result_list)
            del result_list[:]

        if count % 1000 == 0:
            log.info("当前处理进度: count = {}".format(count))

    if len(result_list) > 0:
        source_db.insert_batch_data(source_table_name, result_list)
        del result_list[:]
    log.info("处理完成...")


if __name__ == '__main__':
    main()
